package com.enqualcomm.kids.mvp.login.wx.presenter;

import android.text.TextUtils;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.config.userdefault.UserDefault;
import com.enqualcomm.kids.mvp.login.wx.model.WXRegistModel;
import com.enqualcomm.kids.mvp.login.wx.model.WXRegistModelImpl;
import com.enqualcomm.kids.mvp.login.wx.view.BindPhoneView;
import com.enqualcomm.kids.network.socket.response.BindUserRelationResult;
import com.enqualcomm.kids.network.socket.response.LoginOtherResult;
import com.enqualcomm.kids.network.socket.response.LoginOtherUpdateResult;

/**
 * Created by Sunzhigang on 2016/9/8.
 */
public class WXRegisterPresenter implements Presenter, OnWXRegistListener {

    BindPhoneView view;
    WXRegistModel model;

    public WXRegisterPresenter(BindPhoneView view) {
        this.view = view;
        model = new WXRegistModelImpl();
    }

    @Override
    public void start() {

        if (view.getRegistType()) {
            bindOldAccount();
        } else {
            wxRegist();
        }
    }


    @Override
    public void registSuccess(LoginOtherResult response) {
        AppDefault appDefault = new AppDefault();
        appDefault.setUserid(response.result.userid);
        appDefault.setUserkey(response.result.userkey);
        appDefault.setLoginByWX(true);
        appDefault.setWXLoginType(500); //新注册没有绑定账号没有绑定手表
        appDefault.setProducts(response.result.products + "");
        appDefault.setApiUsername(response.result.username);
        new UserDefault(response.result.userid).setLoginOtherResult(response);
        view.wxRegistSuccess(true);
    }

    @Override
    public void registError() {
        view.wxRegistSuccess(false);
    }

    @Override
    public void bindSuccess(LoginOtherResult response) {
        AppDefault appDefault = new AppDefault();
        appDefault.setUserid(response.result.userid);
        appDefault.setUserkey(response.result.userkey);
        appDefault.setUsername(response.result.username);
        appDefault.setWXLoginType(Integer.parseInt(response.result.type));
        appDefault.setLoginByWX(true);
        appDefault.setProducts(response.result.products + "");
        appDefault.setApiUsername(response.result.username);
        new UserDefault(response.result.userid).setLoginOtherResult(response);
        view.bindOldAccountSuccess(true);
    }

    @Override
    public void bindtError(int id) {
        view.bindOldAccountSuccess(false);
    }

    public void wxRegist() {
        if (!TextUtils.isEmpty(view.getPhoneNumber()) && !TextUtils.isEmpty(view.getCountryCode())) {
            model.newRegist(view.getPhoneNumber(), view.getCountryCode(), this);
        } else if (TextUtils.isEmpty(view.getCountryCode())) {
            view.showMessage(R.string.login_choice_country);
        } else {
            view.showMessage(R.string.input_your_number);
        }
    }

    public void bindOldAccount() {
        if (!TextUtils.isEmpty(view.getPhoneNumber()) && !TextUtils.isEmpty(view.getCountryCode()) && !TextUtils.isEmpty(view.getPassword())) {
            model.oldBind(view.getPhoneNumber(), view.getCountryCode(), view.getPassword(), this);
        } else if (TextUtils.isEmpty(view.getCountryCode())) {
            view.showMessage(R.string.login_choice_country);
        } else if (TextUtils.isEmpty(view.getPhoneNumber())) {
            view.showMessage(R.string.input_your_number);
        } else {
            view.showMessage(R.string.login_input_password);
        }
    }


}
